package com.miss.spjyx

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.design.widget.TabLayout
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import com.miss.spjyx.utils.UHfUtils
import java.lang.ref.WeakReference

/**
 * 首页
 *
 * @author wl
 * @since 2019/08/05 23:31
 */
class IndexActivity : AppCompatActivity() {
    private val MESSAGE_SUCCESS = 0
    private val MESSAGE_FAIL = 1
    private val mHandler = ConnectHandler(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_index)

        val frag_ckd = supportFragmentManager.findFragmentById(R.id.frag_ckd)!!
        val frag_rkd = supportFragmentManager.findFragmentById(R.id.frag_rkd)!!
        val frag_device = supportFragmentManager.findFragmentById(R.id.frag_device)!!

        supportFragmentManager.beginTransaction()
            .hide(frag_rkd)
            .hide(frag_device)
            .commitAllowingStateLoss()

        val tab = findViewById<TabLayout>(R.id.tab)
        tab.addOnTabSelectedListener(object : TabLayout.BaseOnTabSelectedListener<TabLayout.Tab> {
            override fun onTabSelected(t: TabLayout.Tab) {
                when (t.position) {
                    0 -> supportFragmentManager.beginTransaction()
                        .show(frag_ckd)
                        .hide(frag_rkd)
                        .hide(frag_device)
                        .commitAllowingStateLoss()
                    1 -> supportFragmentManager.beginTransaction()
                        .hide(frag_ckd)
                        .show(frag_rkd)
                        .hide(frag_device)
                        .commitAllowingStateLoss()
                    2 -> supportFragmentManager.beginTransaction()
                        .hide(frag_ckd)
                        .hide(frag_rkd)
                        .show(frag_device)
                        .commitAllowingStateLoss()
                }
            }

            override fun onTabReselected(t: TabLayout.Tab) {
            }

            override fun onTabUnselected(t: TabLayout.Tab) {
            }
        })

        UHfUtils.init()
        Thread(Runnable {
            try {
                val result = UHfUtils.openUHf()
                if (result == 0) {
                    mHandler.sendEmptyMessage(MESSAGE_SUCCESS)
                } else {
                    mHandler.sendEmptyMessage(MESSAGE_FAIL)
                }
            } catch (e: Exception) {
                mHandler.sendEmptyMessage(MESSAGE_FAIL)
            }
        }).start()
    }

    override fun onDestroy() {
        try {
            UHfUtils.closeUHf()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        super.onDestroy()
    }

    private class ConnectHandler internal constructor(activity: IndexActivity) : Handler() {
        private val mReference: WeakReference<IndexActivity> = WeakReference(activity)

        override fun handleMessage(msg: Message) {
            val mActivity = mReference.get() ?: return
            if (msg.what == mActivity.MESSAGE_SUCCESS) {
                Toast.makeText(mActivity, "端口连接成功", Toast.LENGTH_SHORT).show()
            } else if (msg.what == mActivity.MESSAGE_FAIL) {
                Toast.makeText(mActivity, "端口连接失败", Toast.LENGTH_SHORT).show()
            }
        }
    }
}